#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
c = 10
c = c / 3
c = c * 2.999999999999999999999999999999999
print('你好',c)
print(ord('A'))
print(chr(65))
print('\u4e2d')

s1 = 72
s2 = 85
r = (85 - 72) / 72 *100

print('晓明提升了%.2f%%' % r)


classmates = ['Alice', 'Bob', 'Cill']
print(classmates[0])
print(classmates[len(classmates)-1], classmates[-1])

classmates.insert(2, 'David')
classmates.insert(2, 'David')
classmates.pop(2)
classmates.pop(2)
classmates.append("haha")
print(classmates)

group = ['class1', classmates]
bgroup = ['gp1', group]
print(group)
print(bgroup)

t = ()
print(t)

t = (1)
print(t)

t = (1,)
print(t)

t = (1, 2)
print(t)

inp = input('please input your birth year: ')
birhtY = int(inp)
if birhtY < 1990:
	print('90前')
else:
	print('90后')

print('begin to calculate\n')
for gg in bgroup:
	print(gg)

sum = 0
for x in range(101):
	sum = sum + x
	print(sum)
	if sum > 20:
		print('end')
		break

d = {'m':'dd', 'n':2, 'o':bgroup}
print(d['o'])

print(hex(int(input('please input a number:'))))

def my_abs(x):
	if x > 0:
		return x
	else:
		return -x

print(my_abs(-23))

def my_habs(x):
	if not isinstance(x, (int, float)):
		raise TypeError('bad operand type')
	if x >= 0:
		return x
	else:
		return -x


import math

def move(x, y, step = 0, angle = 0):
	nx = x + step * math.cos(angle)
	ny = y - step * math.sin(angle)
	return nx, ny

print(move(12, 34))

# 计算一元二次方程组的解
#BEGIN

def quaratic(a, b, c):

	if not isinstance(a, (int, float)):
		raise TypeError('input isinstance error')
	if not isinstance(b, (int, float)):
		raise TypeError('input isinstance error')
	if not isinstance(c, (int, float)):
		raise TypeError('input isinstance error')

	if a == 0:
		return 'input error'

	temp = (b * b - 4 * a * c )

	if int(temp) < 0:
		return 'no ans'
	elif int(temp) == 0:
		return 'one ans', (-b / 2 * a)
	else:
		ans1 = (-b + math.sqrt(int(temp)) / 2 * a)
		ans2 = (-b - math.sqrt(int(temp)) / 2 * a)
	return ans1, ans2

print(quaratic(1, 4, 8))
print(quaratic(0, 2, 3))
print(quaratic(1, 8, 4))
print(quaratic(1, 4, 4))

#END

def add(L = []):
	L.append('end')
	return L

print(add([1, 2]))
print(add([1]))
print(add())
print(add())

def calc(*numbers):
	sum = 0
	for n in numbers:
		sum = sum + n * n
	return sum

te = [1, 2, 3]
print(calc(*te))

def calc(numbers):
	sum = 0
	for n in numbers:
		sum = sum + n * n
	return sum

te = [1, 2, 3]
print(calc(te))

def hello(gretting, *args):
	if (len(args) == 0):
		print('%s!' % gretting)
	else:
		print('%s, %s!' % (gretting, ''.join(args)))

hello('Hi')
hello('Hi', 'Sarah')
hello('Hello', 'Michael', 'Bob', 'Adam')

names = ('Bart', 'Lisa')
hello('Hello', *names)

def print_scores(**kw):
	print('	Name	Score')
	print('---------------------------------')
	for name, score in kw.items():
		print('%12s	%s' % (name, score))
	print()
print_scores(Adam = '99', Lisa = 88, Bart = 77)

data = {
	'Adam Lee':'99',
	'Lisa S':88,
	'F.Bart':77,
	'David Dong':100
}

print_scores(**data)

def print_info(name, *, gender, city='Kaifeng', age):
	print('Personal Info')
	print('-------------------')
	print('Name: %s' % name)
	print('Gender: %s' % gender)
	print('City: %s' % city)
	print('Age: %s' % age)
	print()

print_info('Bob', gender = 'male', age = 20)
print_info('Lisa', gender = 'female', city = 'Shanghai', age = 18)

args = ('Bob')
kw = {'gender':'male', 'city':'beijing', 'age':20}

print_info(args, **kw)

#递归
def fact(n):
	if n == 1:
		return 1
	return n * fact(n - 1)

print(fact(10))

#尾递归
def fact_back(n):
	return fact_back_iter(n, 1)

def fact_back_iter(num, outAns):
	if num == 1:
		return outAns
	return fact_back_iter(num - 1, num * outAns)

print(fact_back(10))

L = list(range(100))
print(L)

L1 = ['Hello', 'World', 18, '19', 'Apple', None]
L2 = []

for s in L1:
	if(isinstance(s, str) == True):
		L2.append(s.lower())
	else:
		L2.append(s)
print(L2)

###########################
g = (x * x for x in range(10))
for n in g:
	print(n)

###############
def fib(num):
	n, a, b = 0, 0, 1
	while n < num:
		yield b
		a, b = b, a+b
		n = n + 1
	return 'done'

for nn in fib(6):
	print(nn)

#把函数最为参数传入，这样的函数称为高阶函数，函数式编程就是指这种高度抽象的编程范式

def f(x):
	return x * x
l1 = [1, 3, 5, 7, 9]
r = map(f,l1)
for m in r:
	print(m)

from functools import reduce

def add(x, y):
	return x + y
print(reduce(add, l1))




### test1
def triangles_1():
	l1 = [1]
	while True:
		yield l1
		l1_temp1 = [0] + l1
		l1_temp2 = l1 + [0]
		l1 = [(l1_temp1[i] + l1_temp2[i]) for i in range(len(l1_temp2))]

n = 0
for list1 in triangles_1():
	print(list1)
	n = n + 1
	if n == 10:
		break

### test1 simplify
def triangles_1_sim():
	l1 = [1]
	while True:
		yield l1
		l1 = [([0] + l1)[i] + (l1 + [0])[i] for i in range(len(l1) + 1)]

n = 0
for list1 in triangles_1_sim():
	print(list1)
	n = n + 1
	if n == 10:
		break

### test2
def triangles_2(n):
	num = 1
	l1 = [1]
	while num <= n:
		yield l1
		num = num + 1
		l1 = [([0] + l1)[i] + (l1 + [0])[i] for i in range(len(l1) + 1)]

for list1 in triangles_2(10):
	print(list1)

'''
# import numpy as np
# import datetime
# import time
# from os import listdir
#
# data = np.array([
#     [1, 2, 2],
#     [0, 2, 1],
#     [2, 1, 4],
#     [3, 1, 4]])
#
# pData1 = np.sum(data, axis=0)
# print(pData1)
# pData2 = data.sum(axis=0)
# print(pData2)
# data2 = np.random.randint(0, 5, [4, 3, 2, 3])
# print(data2)
#
# time.sleep(2)
#
# now = datetime.datetime.now()
# print(now)
# print(np.zeros(8))
# print(listdir('D:\WorkSpaces\pythonWorkspace'))
# now2 = datetime.datetime.now()
# print(now2)
# print(now2 - now)

L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]

def sort_by_name(x):
    return x[0].lower()

def sort_by_score(x):
    return x[1]

print(sorted(L, key = sort_by_name))

print(sorted(L, key = sort_by_score, reverse=True))

def lazy_sum(*args):
    def sum():
        ax = 0
        for n in args:
            ax = ax + n
        return ax
    return sum

f = lazy_sum(1, 3, 5, 7, 9)
print(f)
print(f())

print('----------------------')
def log(func):
    def wrapper(*args, **kw):
        print('call %s():' % func.__name__)
        return func(*args, **kw)
    return wrapper

@log
def now():
    print('2015-3-25')

print(now())